package com.peanut.controller;

import com.mvc.annotation.*;
import com.peanut.entity.Channel;
import com.peanut.entity.JsonResult;
import com.peanut.entity.dto.QueryChannel;
import com.peanut.service.ChannelService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class ChannelController {
	@Autowired
	private ChannelService service;
	
	@ResponseBody
	@RequestMapping("/channel/query.do")
	public JsonResult queryChannelType(@RequestBody QueryChannel queryChannel){
		List<Channel> channels = service.queryChannel(queryChannel);
		return JsonResult.success(channels);
	}
	
	@ResponseBody
	@RequestMapping("/channel/update.do")
	public JsonResult updateChannelType(@RequestBody @Valid Channel channel){
		if(channel.getId() == null){
			channel.setCreateTime(LocalDateTime.now());
			if (service.add(channel)) {
				return JsonResult.success();
			}
		}else{
			if (service.updateById(channel)) {
				return JsonResult.success();
			}
		}
		return JsonResult.fail();
	}
	
	@ResponseBody
	@RequestMapping("/channel/delete.do")
	public JsonResult deleteChannelType(@RequestParam(required = true) int id){
		if (service.delete(id)) {
			return JsonResult.success();
		}
		return JsonResult.fail();
	}
}
